// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  console.log("onLaunch")
  console.log(this.globalData.userInfo)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        //获取code
        var code = res.code;
        console.log(code)
        //debugger;
        if (code) {
          //发送code到后台，分析openid
          wx.request({
            url: 'http://127.0.0.1:8080/user/miniapp/login?code=' + code,
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success:  (res) =>{
              console.log(res)
              //debugger;
              if (res.data.success&&res.data.data) {
                  this.globalData.userId = res.data.data.id;
              } else {
                //status
                wx.showToast({
                  title: '请先登录哦',
                })
                wx.switchTab({
                  url: '/pages/index/index',
                })
              } 
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    host: "http://localhost:8080",
    category: {},
    cart: [],
    currentRadio:0,
    address: [],
    userId:null,
  }
})